﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

namespace WebApplication1
{
    public partial class TrangTongTien : System.Web.UI.Page
    {
        sv_QuanLyNhaSach.AdapterClient service = new sv_QuanLyNhaSach.AdapterClient();
        string MaTKOL = "";
        string SoHD = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Browser.Cookies)
            {
                if (Request.Cookies.Get("Login") != null)
                {
                    HttpCookie MyCookies = Request.Cookies["Login"];
                    if (MyCookies["UserName"] != null)
                    {
                        MaTKOL = LayMaTK(MyCookies["UserName"].ToString());
                    }
                }
                else if (Session["UserName"] != null)
                {
                    MaTKOL = LayMaTK(Session["UserName"].ToString());
                }
            }
            if (MaTKOL != "")
            {
                SoHD = Request.QueryString["SoHD"];
                Load_TTT(SoHD);
            }
            else
                Response.Redirect("TrangChu.aspx");

        }
        private void Load_TTT(string madhol)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select tongtien from dathangonline where madhol='" + SoHD + "' ")));
            if (ds.Tables.Count > 0)
            {
                if (!IsPostBack)
                {
                    lb_tongtien.Text = double.Parse(ds.Tables[0].Rows[0][0].ToString()).ToString("#,##0 VNĐ");
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Số đơn hàng không tồn tại.'); ", true);
                Response.Redirect("TrangChu.aspx");
            }

        }
        private string LayMaTK(string p)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(new StringReader(service.selectqueryByText("select matkol from TaiKhoanOnline where TenDangNhap='" + p + "' ")));
            if (ds.Tables.Count > 0)
            {
                return ds.Tables[0].Rows[0][0].ToString();
            }
            return "";
        }
    }
}